from marshmallow import Schema, fields, ValidationError, validate, validates_schema
from utils.ext import inner_db

class MachineStateSchema(Schema):
    id = fields.String(required=True)

class MachineAddSchema(Schema):
    host_ip = fields.String(required=True)
    # sshport = fields.Integer()
    # username = fields.String()
    # password = fields.String()
    # machine_type = fields.String(required=True)
    force = fields.Boolean(missing=False)

class MachineUpdateSchema(Schema):
    host_ip = fields.String(required=True)
    ssh_port = fields.Integer()
    username = fields.String()
    password = fields.String()
    machine_type = fields.String()


class MachineDeleteSchema(Schema):
    host_ip = fields.String(required=True)

class MachineConfigAddSchema(Schema):
    host_ip = fields.String(required=True)
    ssh_port = fields.Integer(required=True)
    username = fields.String(required=True)
    password = fields.String(required=True)
    machine_type = fields.String()


class MachineConfigUpdateSchema(Schema):
    host_ip = fields.String(required=True)
    ssh_port = fields.Integer()
    username = fields.String()
    password = fields.String()
    machine_type = fields.String()

class MachineConfigDeleteSchema(Schema):
    host_ip = fields.String(required=True)

class DownloadArgsSchema(Schema):
    id = fields.Integer(required=True)

class MachineConfigGetSchema(Schema):
    per_page_items = fields.Integer(required=True)
    page_index = fields.Integer(required=True)

class NodeOpertaion(inner_db.Model):
    __bind_key__ = 'management'
    __tablename__ = 'node_operation'

    id = inner_db.Column(inner_db.BigInteger, primary_key=True, autoincrement=True, nullable=False)
    task_id = inner_db.Column(inner_db.String(100), nullable=False)
    node_task_id = inner_db.Column(inner_db.String(100), nullable=False)
    node_ip = inner_db.Column(inner_db.String(100), nullable=False)
    operation = inner_db.Column(inner_db.String(10), nullable=False)
    finished = inner_db.Column(inner_db.Boolean, default=False)
    create_time = inner_db.Column(inner_db.BigInteger)
    update_time = inner_db.Column(inner_db.BigInteger)
    exit_code =  inner_db.Column(inner_db.Integer)

    def get_data_info(self):
        return dict(id = self.id,
                    task_id = self.task_id,
                    node_ip = self.node_ip,
                    operation = self.operation,
                    create_time = self.create_time,
                    update_time = self.update_time)

    def __str__(self):
        return "{}".format(self.get_data_info())

    def __repr__(self):
        return "{}".format(self.get_data_info())

class NodeConfig(inner_db.Model):
    __bind_key__ = 'management'
    __tablename__ = 'node_config'

    node_ip = inner_db.Column(inner_db.String(100), primary_key=True, nullable=False)
    ssh_user = inner_db.Column(inner_db.String(10), nullable=False)
    ssh_port = inner_db.Column(inner_db.Integer, default=22)
    ssh_password = inner_db.Column(inner_db.String(100), nullable=False)
    #os = inner_db.Column(inner_db.String(50))
    machine_type = inner_db.Column(inner_db.String(50))

    def get_data_info(self):
        return dict(node_ip = self.node_ip,
                    ssh_user = self.ssh_user,
                    ssh_port = self.ssh_port,
                    ssh_password = self.ssh_password,
                    machine_type = self.machine_type
                    )

    def __str__(self):
        return "{}".format(self.get_data_info())

    def __repr__(self):
        return "{}".format(self.get_data_info())
